package com.md.youyouhui.ui.home;

import android.databinding.DataBindingUtil;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.md.youyouhui.R;
import com.md.youyouhui.databinding.ActivityQuestionMoreBinding;
import com.md.youyouhui.net.response.QuestionResponse;
import com.md.youyouhui.ui.BaseActivity;
import com.md.youyouhui.utils.ToastUtils;
import com.md.youyouhui.view.MainViewModel;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by AdministratorZJ
 * on 2019/4/15.
 */
public class QuestionMoreActivity extends BaseActivity {
    private ActivityQuestionMoreBinding moreBinding;
    private List<String> titles = new ArrayList<>();
    private List<String> childContent;
    private List<List> childTitle = new ArrayList<>();
    private MyAdapter adapter;
    private MainViewModel viewModel;

    @Override
    public void setContentLayout() {
        moreBinding = DataBindingUtil.setContentView(this, R.layout.activity_question_more);
        viewModel = new MainViewModel();
    }

    @Override
    public void initView() {
        setSupportActionBar(moreBinding.toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public void initData() {
        getQuestionList();
    }

    private void getQuestionList() {
        viewModel.question(1, 100).observe(this, questionResponse -> {
            assert questionResponse != null;
            switch (questionResponse.status) {
                case LOADING:
                    moreBinding.setProgress(false);
                    break;
                case SUCCESS:
                    moreBinding.setProgress(true);
                    QuestionResponse data = questionResponse.data;
                    if (null == data) {
                        return;
                    }
                    List<QuestionResponse.Rows> rows = data.rows;
                    for (int i = 0; i < rows.size(); i++) {
                        titles.add(rows.get(i).title);
                        childContent = new ArrayList<>();
                        childContent.add(rows.get(i).content);
                        childTitle.add(childContent);
                    }
                    adapter = new MyAdapter(titles, childTitle);
                    moreBinding.questList.setAdapter(adapter);
                    break;
                case ERROR:
                    moreBinding.setProgress(true);
                    ToastUtils.errMake(this, questionResponse.errorCode);
                    break;
                default:
                    break;
            }
        });
    }

    class MyAdapter extends BaseExpandableListAdapter {
        List<String> titleList;
        List<List> childTitle;

        public MyAdapter(List<String> title, List<List> childTitle) {
            this.titleList = title;
            this.childTitle = childTitle;
        }

        @Override
        public int getGroupCount() {
            return titleList.size();
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            List list = childTitle.get(groupPosition);
            return list.size();
        }

        @Override
        public Object getGroup(int groupPosition) {
            return titleList.get(groupPosition);
        }

        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return childTitle.get(groupPosition).get(childPosition);
        }

        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

        @Override
        public boolean hasStableIds() {
            return true;
        }

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
            View view;
            if (convertView == null) {
                view = View.inflate(ct, R.layout.adapter_question_more, null);
            } else {
                view = convertView;
            }
            TextView title = view.findViewById(R.id.questTitle);
            title.setText(titleList.get(groupPosition));
            return view;
        }

        @Override
        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
            View view;
            if (convertView == null) {
                view = View.inflate(ct, R.layout.adapter_question_more, null);
            } else {
                view = convertView;
            }
            TextView title = view.findViewById(R.id.questTitle);
            List<String> list = childTitle.get(groupPosition);
            title.setText(list.get(childPosition));
            return view;
        }

        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }

        @Override
        public boolean areAllItemsEnabled() {
            return false;
        }

        @Override
        public boolean isEmpty() {
            return false;
        }
    }
}

